//
//  varietiesView.m
//  XYMaterial
//
//  Created by 肖扬 on 15/12/15.
//  Copyright © 2015年 肖扬. All rights reserved.
//

#import "varietiesView.h"

@implementation varietiesView
@synthesize mainTable,mainData,isShow,delegate,selectVarieties;
-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.isShow = NO;
        self.mainData = [NSMutableArray array];
        self.mainTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SWIDTH, frame.size.height)];
        [self.mainTable setBackgroundColor:[UIColor clearColor]];
        [self.mainTable setDelegate:self];
        [self.mainTable setDataSource:self];
        [self addSubview:self.mainTable];
        if ([self.mainTable respondsToSelector:@selector(setSeparatorInset:)])
        {
            [self.mainTable setSeparatorInset:UIEdgeInsetsZero];
        }
        if ([self.mainTable respondsToSelector:@selector(setLayoutMargins:)])
        {
            [self.mainTable setLayoutMargins:UIEdgeInsetsZero];
        }
    }
    return self;
}
-(void)reloadView:(NSMutableArray *)arr{
    [self.mainData removeAllObjects];
    [self.mainData addObjectsFromArray:arr];
    plankModel *model = [[plankModel alloc]init];
    model.material_id = @"";
    model.name = @"全部";
    [self.mainData insertObject:model atIndex:0];
    [self.mainTable reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [mainData count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static  NSString * showUserInfoCellIdentifier = @ "ShowUserInfoCell" ;
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:showUserInfoCellIdentifier];
    if  (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                       reuseIdentifier:showUserInfoCellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    plankModel *model = [self.mainData objectAtIndex:indexPath.row];
    cell.textLabel.text= model.name;
    cell.textLabel.textColor = [UIColor grayColor];
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        // 取消标记
        cell.accessoryType  = UITableViewCellAccessoryNone;
    }
    if ([model.name isEqualToString:selectVarieties]) {
        cell.textLabel.textColor = UIColorFromRGB(0x2578c3);
        cell.accessoryType  = UITableViewCellAccessoryCheckmark;
    }else{
        cell.textLabel.textColor = GRAYColor;
    }
    if ([selectVarieties isEqualToString:@"品种"]) {
        if ([model.name isEqualToString:@"全部"]) {
            cell.textLabel.textColor = UIColorFromRGB(0x2578c3);
            cell.accessoryType  = UITableViewCellAccessoryCheckmark;
        }else{
            cell.textLabel.textColor = GRAYColor;
        }
    }
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //TODO:点击事件
    plankModel *model = [self.mainData objectAtIndex:indexPath.row];
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(dissMissVarietiesView:)]) {
        [self.delegate dissMissVarietiesView:model];
    }
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)])
    {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)])
    {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
//-(void)dealloc{
//    [self.mainData removeAllObjects];
//    self.mainData = nil;
//}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
